<?php

namespace app\controllers;

use Yii;
use app\tools\Re;
use app\models\User;
use app\models\Good;
use app\models\ShoppingCart;

class ShoppingCartController extends BaseController
{
    //加入购物车
    public function actionAdd(){
      $openid = Yii::$app->request->post('openid', '');
      $good_id = Yii::$app->request->post('good_id', 0);
      $num = Yii::$app->request->post('num', 0);
      $good_info = Good::find()->where(['id' => $good_id, 'status' => 1])->one();
      if (!$good_info) {
        return Re::data(null, '商品信息不存在', Re::ERROR);
      }
      $user_info = User::find()->where(['openid' => $openid, 'status' => 1])->one();
      if (!$user_info) {
        return Re::data(null, '用户信息读取错误', Re::ERROR);
      }
      if ($good->store < $num) {
        return Re::data(null, '商品库存不足，无法加入购物车', Re::ERROR);
      }
      $user_cart = ShoppingCart::find()->where(['user_id' => $user_info->id, 'good_id' => $good_id, 'status' => 1])->one();
      if (!$user_cart) {
        $user_cart = new ShoppingCart();
        $user_cart->user_id = $user_info->id;
        $user_cart->good_id = $good_id;
        $user_cart->num = $num;
      } else {
        $user_cart->num += $num;
      }
      $user_cart->created_at = time();
      if (!$user_cart->save()) {
        return Re::data(null, $user_cart->showErrors(), Re::ERROR);
      }
      return Re::data();
    }

    //我的购物车
    public function actionMyCart(){
      $openid = Yii::$app->request->post('openid', '');
      $user_info = User::find()->where(['openid' => $openid, 'status' => 1])->one();
      if (!$user_info) {
        return Re::data(null, '用户信息读取错误', Re::ERROR);
      }
      $user_cart = ShoppingCart::find()->where(['user_id' => $user_info->id, 'status' => 1])->all();
      $user_cart_data = [];
      if ($user_cart) {
        foreach ($user_cart as $v) {
          $user_cart_data[] = [
            'id' => $v->id,
            'good_id' => $v->good_id,
            'good_name' => mb_strlen($v->good->name, 'UTF-8') >= 20 ? mb_substr($v->good->name, '0', 20, 'UTF-8') . '...' : $v->good->name,
            'good_img' => $v->good->img,
            'good_price' => $v->good->price,
            'num' => $v->num,
            'created_time' => date('Y-m-d H:i:s', $v->created_at),
          ];
        }
      }
      $shopping_cart_count = ShoppingCart::find()->where(['user_id' => $user_info->id, 'status' => 1])->SUM('num');
      $shopping_cart_count = empty($shopping_cart_count) ? 0 : (int)$shopping_cart_count;
      return Re::data(['data' => $user_cart_data, 'shopping_cart_count' => $shopping_cart_count]);
    }

    //删除购物车
    public function actionDeleteCart(){
      $ids = Yii::$app->request->post('ids', '');
      if (empty($ids)) {
        return Re::data(null, '请选择购物车商品', Re::ERROR);
      }
      $id_arr = explode(',', $ids);
      ShoppingCart::updateAll(['status' => -1], ['id' => $id_arr]);
      return Re::data();
    }

    //修改购物车商品数量
    public function actionUpdateCart(){
      $cart_info = json_decode(Yii::$app->request->post('cart_info', ''));
      foreach ($cart_info as $v) {
        $shoppingCart = ShoppingCart::findOne($v->id);
        if ($shoppingCart) {
          $shoppingCart->num = $v->num;
          $shoppingCart->save();
        }
      }
      return Re::data();
    }

    //购物车商品总数
    public function actionCartCount(){
      $openid = Yii::$app->request->post('openid', '');
      $user_info = User::find()->where(['openid' => $openid, 'status' => 1])->one();
      if (!$user_info) {
        return Re::data(null, '用户信息读取错误', Re::ERROR);
      }
      $shopping_cart_count = ShoppingCart::find()->where(['user_id' => $user_info->id, 'status' => 1])->SUM('num');
      $shopping_cart_count = empty($shopping_cart_count) ? 0 : (int)$shopping_cart_count;
      return Re::data(['data' => $shopping_cart_count]);
    }
}
